﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 编译原理
{
    public static class 实验一
    {
        //关键字
        static List<string> Keywords { get; set; } = new List<string>();
        //标识符
        static List<string> Identifiers { get; set; } = new List<string>();
        //常数
        static List<int> Constants { get; set; } = new List<int>();
        //运算符
        static List<char> Operators { get; set; } = new List<char>();
        //界符
        static List<char> Boundaries { get; set; } = new List<char>();
        static string str = "";
        static bool isConstant = false;
        static void Init()
        {
            Keywords.AddRange("int char float void const if else do while scanf printf return mian read".Split(' '));
            Operators.AddRange("+-*/%=><&|!");
            Boundaries.AddRange("(){};,\"'");
        }
        public static void 词法分析()
        {
            Init();
            Console.WriteLine("请输入要分析的句子：");
            string sstr = Console.ReadLine();
            Console.WriteLine("分析结果：");
            Analyzer(sstr);
            Console.ReadKey();
        }
        public static void Analyzer(string sstr)
        {
            if (sstr.Length == 0) return;
            foreach (char c in sstr)
            {
                if (c == ' ')
                {
                    Judge();
                }
                else if (Operators.Contains(c))
                {
                    Judge();
                    PrintOperator(c);
                }
                else if (Boundaries.Contains(c))
                {
                    Judge();
                    PrintBoundary(c);
                }
                //标识符或者关键字
                else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
                {
                    str += c;
                }
                //数字
                else if (c >= '0' && c <= '9')
                {
                    if (str.Length == 0) { isConstant = true; }
                }
                else
                {
                    Judge();
                    Console.WriteLine($"奇怪的字符  {c}");
                }
            }
        }
        public static void Reset()
        {
            str = string.Empty;
            isConstant = false;
        }
        public static void Judge()
        {
            if(str.Length == 0) return;
            if (isConstant) { PrintConstant(str); }
            else if (Keywords.Contains(str)) { PrintKeyword(str); }
            else { PrintIdentifier(str); }
            Reset();
        }
        public static void PrintKeyword(string s)
        {
            Console.WriteLine($" {s}\t\t 1\t {Keywords.IndexOf(s)} ");
        }
        public static void PrintIdentifier(string s)
        {
            if (!Identifiers.Contains(s))
            {
                Identifiers.Add(s);
            }
            Console.WriteLine($" {s}\t\t 2\t {Identifiers.IndexOf(s)}");
        }
        public static void PrintConstant(string s)
        {
            int a = Convert.ToInt32(s);
            if (!Constants.Contains(a))
            {
                Constants.Add(a);
            }
            Console.WriteLine($" {a}\t\t 3\t {Constants.IndexOf(a)}");
        }
        public static void PrintOperator(char c)
        {
            Console.WriteLine($" {c}\t\t 4\t {Operators.IndexOf(c)}");
        }
        public static void PrintBoundary(char c)
        {
            Console.WriteLine($" {c}\t\t 5\t {Boundaries.IndexOf(c)}");
        }
    }
}
